
package com.scu.controller;

import com.scu.pojo.Block;
import com.scu.service.BlockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class   BlockController {
    @Autowired
    private BlockService blockService;

    //获取所有版块列表信息
    @RequestMapping("getBlockList")
    @ResponseBody
    public Map getBlockList(Block block){
        HashMap<String,Object> map = new HashMap<>();
        List<Block> blockList = blockService.getBlockList();    //获取板块信息
        int count = blockService.getBlockCount(block);       //获取板块数
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",blockList);
        return map;
    }

    //分页获取版块信息
    @RequestMapping("getBlockListByPage")
    @ResponseBody
    public Map getBlockListByPage(Block block,int page, int limit){

        HashMap<String,Object> map = new HashMap<>();
        List<Block> blockList = blockService.getBlockListByPage(block,page,limit);
        int count = blockService.getBlockCount(block);       //获取板块数
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",blockList);
        return map;
    }

    //获取版块数
    @RequestMapping("getBlockNum")
    @ResponseBody
    public Map getBlockNum(){
        HashMap<String,Object> map = new HashMap<>();

        Block block = new Block();
        int count = blockService.getBlockCount(block);       //获取板块数
        map.put("code",200);
        map.put("msg","");
        map.put("count",count);
        return map;
    }

    //新增版块
    @RequestMapping("addBlock")
    @ResponseBody
    public Map addBlock(Block block){
        Block newblock = new Block();
        HashMap<String,Object> map = new HashMap<>();
        newblock.setB_title(block.getB_title());
        newblock.setB_description(block.getB_description());
//        newblock.setB_topic_num("0");
        int result = blockService.addBlock(newblock);
        map.put("code",200);
        map.put("msg","");

        return map;
    }

    //删除版块
    @RequestMapping("removeBlockById")
    @ResponseBody
    public Map removeBlockById(Block block){

        int result = blockService.removeBlockById(block.getB_id());
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","");

        return map;
    }


    //修改板块信息
    @RequestMapping("redactBlock")
    @ResponseBody
    public Map redactBlock(Block block){
        HashMap<String,Object> map = new HashMap<>();

//        map.put("id",block.getB_id());
//        map.put("title",block.getB_title());
//        map.put("img",block.getB_img());

        int result = blockService.redactBlock(block);

        map.put("code",200);
        map.put("msg","");
        return map;

    }


    //获取当前版块贴子数
    @RequestMapping("getTopicNum")
    @ResponseBody
    public Map getTopicNum(Block block){
        HashMap<String,Object> map = new HashMap<>();

        int topic_num = blockService.getTopicNum(block.getB_id());
        map.put("code",200);
        map.put("msg","");
        map.put("count",topic_num);
        return map;
    }

}
